package senpec.qc.harmony.system.role;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import senpec.qc.harmony.base.mybatisplus.IBaseServiceImpl;
import senpec.qc.harmony.base.mybatisplus.Result;
import senpec.qc.harmony.base.utils.ObjectsUtil;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class RoleServiceImpl extends IBaseServiceImpl<RoleMapper, Role> implements RoleService {

    private final RoleMapper roleMapper;

    @Override
    public Result<Role> insertOne(Role role) {
        Role roleSelected = roleMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getName, role.getName()));
        if (Objects.isNull(roleSelected)) {
            int inserted = roleMapper.insert(role);
            return inserted > 0 ? Result.ok(role) : Result.error();
        } else {
            return Result.error(role).msg("角色名称《" + role.getName() + "》已存在。");
        }
    }

    @Override
    public Result<Role> updateOne(Role role) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getName, role.getName());
        lambdaQueryWrapper.ne(Role::getId, role.getId());
        Role roleSelected = roleMapper.selectOne(lambdaQueryWrapper);
        if (ObjectsUtil.noNull(roleSelected)) {
            return Result.error(Role.builder().build()).msg("角色名称《" + role.getName() + "》已存在。");
        }
        setVersion(role);
        if (roleMapper.updateById(role) > 0) {
            Role roleUpdated = roleMapper.selectById(role);
            setCreatorAndUpdater(roleUpdated);
            return Result.ok(roleUpdated);
        } else {
            return Result.error(role);
        }
    }

    @Override
    public Result<List<Role>> getListByConditions(Role role) {
        List<Role> roles = roleMapper.selectList(setLambdaQueryWrapper(role));
        List<Long> roleIds = roleMapper.selectRoleIdsByUserId(role.getUserId());
        roles.forEach(item -> item.setSelected(roleIds.contains(item.getId())));
        return Result.ok(roles);
    }

    private LambdaQueryWrapper<Role> setLambdaQueryWrapper(Role role) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectsUtil.noNullAndEmpty(role.getType()), Role::getType, role.getType());
        lambdaQueryWrapper.like(ObjectsUtil.noNullAndEmpty(role.getName()), Role::getName, role.getName());
        lambdaQueryWrapper.like(ObjectsUtil.noNullAndEmpty(role.getDescription()), Role::getDescription, role.getDescription());
        return lambdaQueryWrapper;
    }

    @Override
    public Result<?> setRoleIdByUserId(RoleDTO roleDTO) {
        if (roleDTO.getRemove()) {
            roleMapper.insertUserIdAndRoleId(roleDTO.getUserId(), roleDTO.getRoleId());
        } else {
            roleMapper.deleteUserIdAndRoleId(roleDTO.getUserId(), roleDTO.getRoleId());
        }
        return Result.ok();
    }

    @Override
    public List<String> getRoleNameListByUserId(Long userId) {
        return roleMapper.getRoleNameListByUserId(userId);
    }
}